package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.CategoryDao;
import com.example.dao.CommodityDao;
import com.example.dao.UserDao;
import com.example.domain.Category;
import com.example.domain.Commodity;
import com.example.domain.TableData;
import com.example.domain.User;
import com.example.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommodityServiceImpl extends ServiceImpl<CommodityDao, Commodity> implements CommodityService {

    @Autowired
    private CommodityDao commodityDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private UserDao userDao;

    @Override
    public TableData<Commodity> getPage(Integer page, Integer limit, String name, String username) {


        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper();
        lqw.like(!StringUtils.isEmpty(username), User::getUsername, username);

        List<User> userList = userDao.selectList(lqw);

        List userIdList = new ArrayList();
        for (User user : userList) {
            userIdList.add(user.getId());
        }


        LambdaQueryWrapper<Commodity> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name), Commodity::getName, name);
//        lambdaQueryWrapper.in("user_id", userIdList);
        if (userIdList.size() == 0) {
            TableData<Commodity> tableData = new TableData<>();
            tableData.setCount(0L);
            tableData.setCode(0);
            tableData.setMsg("");
            tableData.setData(new ArrayList());
            return tableData;
        }
        System.out.println("********************************************");
        System.out.println(userIdList);
        lambdaQueryWrapper.in(userIdList != null, Commodity::getUserId, userIdList);


        IPage<Commodity> ipage = new Page(page, limit);
        System.out.println("********************************************");
        System.out.println(lambdaQueryWrapper);
        IPage<Commodity> commodityIPage = commodityDao.selectPage(ipage, lambdaQueryWrapper);
        TableData<Commodity> tableData = new TableData<>();
        tableData.setCount(commodityIPage.getTotal());
        tableData.setCode(0);
        tableData.setMsg("");
//        System.out.println(commodityIPage.getRecords());

//        QueryWrapper<Category> qw = new QueryWrapper<>();

        for (Commodity commodity : commodityIPage.getRecords()) {
            Category category = categoryDao.selectById(commodity.getCategoryId());
            User user = userDao.selectById(commodity.getUserId());
            commodity.setCategoryName(category.getCateName());
            commodity.setUsername(user.getUsername());

            User user1 = userDao.selectById(commodity.getPurchaserId());

            if (user1 != null) {
                commodity.setPurchaserName(user1.getUsername());
            }
        }

//        System.out.println(commodityIPage.getRecords());
        tableData.setData(commodityIPage.getRecords());
//        System.out.println(commodityIPage.getRecords());
        return tableData;


    }
}
